package com.quzi.kettle.activitise.main;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.quzi.kettle.GlobalConfig;
import com.quzi.kettle.R;
import com.quzi.kettle.activitise.BaseActivity;
import com.quzi.kettle.bean.ModeItem;
import com.quzi.kettle.datapiece.status.DeviceStatus;
import com.quzi.kettle.utils.MSharePerference;
import com.quzi.kettle.view.TitleBarHandler;
import com.quzi.kettle.view.TitleBarView;

public class ModesActivityold extends BaseActivity {

	private TitleBarView titleView;

	private LinearLayout custom_layout, tea_layout, brew_layout;

	private List<ModeItem> modeLists;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_modes);

		custom_layout = (LinearLayout) findViewById(R.id.custom_layout);
		tea_layout = (LinearLayout) findViewById(R.id.tea_layout);
		brew_layout = (LinearLayout) findViewById(R.id.brew_layout);
		String str = MSharePerference.getInstance(getApplicationContext()).get(
				MODES);
		modeLists = new Gson().fromJson(str, new TypeToken<List<ModeItem>>() {
		}.getType());
		if (modeLists == null) {
			modeLists = new ArrayList<ModeItem>();
		}

		initTitleBar();
		initData();

	}

	DeviceStatus status = new DeviceStatus();

	private void initData() {
		DeviceStatus mStatus = new DeviceStatus();
		mStatus.setFixedTemperatureTime = 120;
		mStatus.get_rid_cl_work = 1;

		View rose = getView(R.drawable.rose_tea, "正山小种", mStatus, 100);
		View green = getView(R.drawable.greentea, "金俊眉", mStatus, 100);
		View red = getView(R.drawable.redtea, "祁门功夫", mStatus, 95);
		View baby = getView(R.drawable.baby_milk, "婴儿奶粉");
		View coffee = getView(R.drawable.coffeetea, "咖啡饮品");

		for (int i = 0; i < modeLists.size(); i++) {
			custom_layout.addView(getCustomView(modeLists.get(i)));
		}
		tea_layout.addView(rose);
		tea_layout.addView(green);
		tea_layout.addView(red);
		tea_layout.addView(getView(R.drawable.rose_tea, "九曲红梅", mStatus, 95));
		tea_layout.addView(getView(R.drawable.rose_tea, "滇红功夫", mStatus, 95));
		tea_layout.addView(getView(R.drawable.rose_tea, "坦洋功夫", mStatus, 95));
		tea_layout.addView(getView(R.drawable.rose_tea, "阿萨姆红茶", mStatus, 100));
		tea_layout.addView(getView(R.drawable.rose_tea, "大吉岭红茶", mStatus, 100));
		tea_layout.addView(getView(R.drawable.rose_tea, "锡兰红茶", mStatus, 100));
		tea_layout.addView(getView(R.drawable.rose_tea, "其他", mStatus, 100));

		brew_layout.addView(baby);
		brew_layout.addView(coffee);

		// cusView.setOnClickListener(new OnClickListener() {
		//
		// @Override
		// public void onClick(View arg0) {
		// // TODO Auto-generated method stub
		// updateMode(new DeviceStatus(), "自定义", 90);
		// finish();
		// }
		// });

		// rose.setOnClickListener(new OnClickListener() {
		//
		// @Override
		// public void onClick(View arg0) {
		// // TODO Auto-generated method stub
		// status.setFixedTemperatureTime = 120;
		// status.get_rid_cl_work = 1;
		// updateMode(status, "玫瑰花茶", 85);
		// finish();
		// }
		// });
		//
		// green.setOnClickListener(new OnClickListener() {
		//
		// @Override
		// public void onClick(View arg0) {
		// // TODO Auto-generated method stub
		// status.setFixedTemperatureTime = 120;
		// status.get_rid_cl_work = 1;
		// updateMode(status, "经典绿茶", 70);
		// finish();
		// }
		// });
		//
		// red.setOnClickListener(new OnClickListener() {
		//
		// @Override
		// public void onClick(View arg0) {
		// // TODO Auto-generated method stub
		// status.setFixedTemperatureTime = 120;
		// status.get_rid_cl_work = 1;
		// updateMode(status, "经典红茶", 85);
		// finish();
		// }
		// });
		//
		// baby.setOnClickListener(new OnClickListener() {
		//
		// @Override
		// public void onClick(View arg0) {
		// status.setFixedTemperatureTime = 120;
		// status.get_rid_cl_work = 1;
		// updateMode(status, "婴儿奶粉", 40);
		// finish();
		// }
		// });
		//
		// coffee.setOnClickListener(new OnClickListener() {
		//
		// @Override
		// public void onClick(View arg0) {
		// // TODO Auto-generated method stub
		// status.setFixedTemperatureTime = 120;
		// status.get_rid_cl_work = 1;
		// updateMode(status, "咖啡饮品", 90);
		// finish();
		// }
		// });

	}

	protected void updateMode(DeviceStatus deviceStatus, String string,
			int targetTemperature) {
		modeStatus = deviceStatus;
		modeStatus.setTemperature = (byte) targetTemperature;
		MSharePerference.getInstance(getApplicationContext()).update(
				GlobalConfig.MODE, string);
		MSharePerference.getInstance(getApplicationContext()).update(
				GlobalConfig.TargetTemp, "" + targetTemperature);
	}

	@Override
	protected void initTitleBar() {
		// TODO Auto-generated method stub
		super.initTitleBar();
		titleView = (TitleBarView) findViewById(R.id.titleview);
		titleView.setTitle(getResources().getString(R.string.mode));
		titleView.setTitleBarClickListener(new TitleBarHandler() {

			@Override
			public void ClickRight() {
				// TODO Auto-generated method stub

			}

			@Override
			public void ClickLeft() {
				// TODO Auto-generated method stub
				finish();
			}
		});
	}

	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub

	}

	View getView(int imageId, String titleStr) {
		LayoutInflater inflater = getLayoutInflater();
		View row;
		row = inflater.inflate(R.layout.mode_item, null);
		TextView title;
		ImageView icon = (ImageView) row.findViewById(R.id.modeicon);
		icon.setImageResource(imageId);
		title = (TextView) row.findViewById(R.id.name);
		title.setText(titleStr);
		return row;
	}

	View getView(int imageId, final String titleStr, final DeviceStatus status,
			final int targetTemperature) {
		LayoutInflater inflater = getLayoutInflater();
		View row;
		row = inflater.inflate(R.layout.mode_item, null);
		TextView title;
		ImageView icon = (ImageView) row.findViewById(R.id.modeicon);
		icon.setImageResource(imageId);
		title = (TextView) row.findViewById(R.id.name);
		title.setText(titleStr);
		row.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				updateMode(status, titleStr, targetTemperature);
				finish();
			}
		});
		return row;
	}

	View getCustomView(final ModeItem item) {
		LayoutInflater inflater = getLayoutInflater();
		View row;
		row = inflater.inflate(R.layout.mode_item, null);
		TextView title;
		ImageView icon = (ImageView) row.findViewById(R.id.modeicon);
		icon.setImageResource(R.drawable.custom);
		title = (TextView) row.findViewById(R.id.name);
		title.setText(item.getModeTitle());
		row.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				DeviceStatus status = new DeviceStatus();
				status.setFixedTemperatureTime = (byte) (item.isNeedWarm() ? 120
						: 0);
				status.get_rid_cl_work = (byte) (item.isNeedBoiled() ? 1 : 0);
				updateMode(status, item.getModeTitle(),
						item.getTargetTemperature());
				Log.d("qiwei", " item = " + item.toString());

				finish();
			}
		});
		row.setOnLongClickListener(new OnLongClickListener() {

			@Override
			public boolean onLongClick(View arg0) {

				View view = View.inflate(getApplicationContext(),
						R.layout.dialog_delete, null);
				TextView cancel = (TextView) view.findViewById(R.id.cancel);
				TextView confirm = (TextView) view.findViewById(R.id.confirm);

				cancel.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View arg0) {
						dialog.dismiss();
					}
				});

				confirm.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View arg0) {
						// 删除
						for (int i = 0; i < modeLists.size(); i++) {
							if (modeLists.get(i).getModeTitle()
									.equals(item.getModeTitle())) {
								modeLists.remove(i);
							}
						}

						// 保存
						String newModeLists = new Gson().toJson(modeLists);
						MSharePerference.getInstance(getApplicationContext())
								.update(MODES, newModeLists);
						dialog.dismiss();

						// 重新加载
						custom_layout.removeAllViews();
						for (int i = 0; i < modeLists.size(); i++) {
							custom_layout.addView(getCustomView(modeLists
									.get(i)));
						}

					}
				});
				showDialog(ModesActivityold.this, view);
				return false;
			}
		});
		return row;
	}

	class DataListAdapter extends BaseAdapter {
		List<ModeItem> modeList;

		public DataListAdapter(List<ModeItem> list) {
			modeList = list;
		}

		public int getCount() {
			return modeList.size();
		}

		public Object getItem(int arg0) {
			return null;
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {

			LayoutInflater inflater = getLayoutInflater();
			View row;
			row = inflater.inflate(R.layout.mode_item, parent, false);
			TextView title;
			ImageView icon = (ImageView) row.findViewById(R.id.modeicon);
			icon.setImageResource(modeList.get(position).getModeImageId());
			title = (TextView) row.findViewById(R.id.name);
			title.setText(modeList.get(position).getModeTitle());

			return (row);
		}
	}

}
